//
//  LivesViewController.swift
//  Swift_MyLive
//
//  Created by admin on 2017/5/12.
//  Copyright © 2017年 admin. All rights reserved.
//

import UIKit

let LiveUrl = "http://live.9158.com/Fans/GetHotLive"
class LivesViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    var tbView : UITableView!
    var array = NSMutableArray()
    override func viewDidLoad() {
        super.viewDidLoad()

        tbView = UITableView.init(frame: CGRect(x:0, y:64, width:WIDTH, height:HEIGHT-64-48))
        tbView.delegate = self;
        tbView.dataSource = self;
        tbView.rowHeight = 120
        self.view.addSubview(tbView)
        tbView.addPullToRefresh { 
            
            self.loadData()
        }
   
        self.loadData()
    }
    
    func loadData() {
        
        AppLicationInfo.shareInstance.POST(getUrl: LiveUrl, parame: nil, isHead: false, success: { (data:NSDictionary) in
            print(data)
            
            let dict = data .dictionaryForKey(key: "data")
            let arr = dict.arrayForKey(key: "list")
           
            for  dic in arr{
                
                 self.array.add(dic)
            }
            self.tbView.reloadData()
            self.tbView.pullToRefreshView.stopAnimating()
        }) { (takes:URLSessionDataTask?, error:NSError) in
            
            self.tbView.pullToRefreshView.stopAnimating()
            print(error)
        }
     
        
    }
    
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return array.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        
        let ID = "cell"
        var cell = tableView.dequeueReusableCell(withIdentifier: ID) as? LivePlayViewCell
        if cell == nil{
            
            cell = LivePlayViewCell.init(style: .default, reuseIdentifier: ID)
        }
        let dict = array[indexPath.row] as! NSDictionary
        
        cell?.setCellInfo(dic:dict)
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
       
        let dict = array[indexPath.row] as! NSDictionary
        let lp = LivePlayViewController()
        lp.dataDic = dict
        self.present(lp, animated: true, completion: nil)
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
